package com.example.petstoreservice.controller;

import com.example.petstoreservice.entity.Order;
import com.example.petstoreservice.entity.OrderItems;
import com.example.petstoreservice.entity.Result;
import com.example.petstoreservice.service.OrderItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/items")
public class OrderItemController {
    @Autowired
    private OrderItemService orderItemService;
    @GetMapping("/all")
    public Result<List<OrderItems>> getAll(){
        Result<List<OrderItems>> result = new Result<>();
        result.setData(orderItemService.getAll());
        return result;
    }
    @GetMapping("/getById/{orderId}")
    public Result<List<OrderItems>> getByOrderId(@PathVariable Long orderId){
        Result<List<OrderItems>> res=new Result<>();
        List<OrderItems> items = orderItemService.getByOrderId(orderId);
        // 遍历所有订单项，将 orderId 转换为字符串类型
        for (OrderItems item : items) {
            if (item.getOrderId() != null) {
                item.setOrderIdString(String.valueOf(item.getOrderId())); // 使用新的字段保存字符串类型的 orderId
            }
        }

        res.setStatusCode("200");
        res.setData(items);
        return res;
    }
}
